package com.brotherly.framework.util;

import java.util.Iterator;
import java.util.LinkedList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class SplitContentParser
{
    protected static final String begin = "$#";
    protected static final String end = "#$";

    private LinkedList<String> sequences = new LinkedList<String>();

    protected String version;

    private int columnSize;

    public SplitContentParser(String content, int type)
    {

        String[] seq = content.split("\\|");

        if (seq.length < 3 || !seq[1].equals(begin))
        {
            version = "";
            return;
        }

        boolean head = true;
        // boolean tableHead = true;

        for (int index = 2; index != seq.length; ++index)
        {
            String field = seq[index].trim();

            if (field.equals(end))
                break;

            if (head)
            {
                if (field.indexOf(',') != -1)
                {
                    head = false;

                    columnSize = Integer.valueOf(seq[index - 1]);

                    StringBuilder builder = new StringBuilder();

                    for (int jndex = 2; jndex < index - 1; ++jndex)
                    {
                        builder.append(seq[jndex]);
                    }

                    version = builder.toString();
                } else
                {
                    continue;
                }
            }
            sequences.add(field);
        }
    }

    private JSONArray toJSONArray(String[] array) throws JSONException
    {
        JSONArray jarray = new JSONArray();
        for (int i = 0; i < columnSize; i++)
        {
            jarray.put(i, array[i]);
        }
        return jarray;
    }

    public JSONObject parseToJSONObject()
    {
        String[] temp = new String[columnSize];
        LinkedList<String[]> rows = new LinkedList<String[]>();
        int index = 0;

        Iterator<String> it = sequences.iterator();
        while (it.hasNext())
        {
            String field = it.next();
            temp[index] = field;

            if (index == columnSize - 1)
            {
                rows.add(temp);
                temp = new String[columnSize];
                index = 0;
            } else
            {
                index++;
            }
        }

        JSONObject jobject = new JSONObject();
        JSONArray dataArray = new JSONArray();

        try
        {
            jobject.put("col", columnSize);
            jobject.put("row", rows.size() - 1);

            for (int i = 0; i < rows.size(); i++)
            {
                String[] rowArray = rows.get(i);
                JSONArray jarray = toJSONArray(rowArray);
                if (i == 0)
                {
                    jobject.put("head", jarray);
                } else
                {
                    dataArray.put(i - 1, jarray);
                }
            }
            jobject.put("rowdata", dataArray);

        }
        catch (JSONException e)
        {
            e.printStackTrace();
        }

        return jobject;

    }
}
